package com.llh.cheap.facades;

import com.llh.cheap.models.CheapUser;
import com.llh.cheap.forms.CheapUserForm;
import com.llh.cheap.services.CheapUserService;
import top.moirae.security.services.SessionService;
import top.moirae.security.models.Session;
import top.moirae.core.AbstractEither;
import top.moirae.core.ErrorMessage;
import top.moirae.security.forms.IdsForm;
import top.moirae.repository.PaginationData;
import top.moirae.repository.PaginationForm;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.util.List;

/**
 * @author lilinhan
 **/
@Named
public class CheapUserFacade {
    private final CheapUserService cheapUserService;
    private final SessionService sessionService;

    @Inject
    public CheapUserFacade(CheapUserService cheapUserService, SessionService sessionService) {
        this.cheapUserService = cheapUserService;
        this.sessionService = sessionService;
    }

    public AbstractEither<ErrorMessage, Long> create(Session session, CheapUserForm.Create form) {
        return sessionService.ifHasPermission(session.getId(), CheapUser.Rules.CREATE, () -> cheapUserService.create(session.getUserId(), form));
    }

    public AbstractEither<ErrorMessage, CheapUser> get(long id) {
        return cheapUserService.get(id);
    }

    public AbstractEither<ErrorMessage, Long> update(Session session, Long id, CheapUserForm.Update form) {
        return sessionService.ifHasPermission(session.getId(), CheapUser.Rules.UPDATE, () -> cheapUserService.update(session.getUserId(), id, form));
    }

    public AbstractEither<ErrorMessage, Integer> delete(Session session, IdsForm ids) {
        return sessionService.ifHasPermission(session.getId(), CheapUser.Rules.DELETE, () -> cheapUserService.delete(ids));
    }

    public AbstractEither<ErrorMessage, List<CheapUser>> list(Session session, CheapUserForm.Search form) {
        return cheapUserService.list(form);
    }

    public AbstractEither<ErrorMessage, PaginationData<CheapUser>> pageList(Session session, CheapUserForm.Search form, PaginationForm paginationForm) {
        return sessionService.ifHasPermission(session.getId(), CheapUser.Rules.READ, () -> cheapUserService.pageList(form, paginationForm));
    }
}

